package cn.slipi.admin.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author lee
 */
public class MoneyUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(MoneyUtil.class);

    public static Double getMoneyDouble(BigDecimal money, Integer bit) {
        if (null != money) {
            if (null == bit) {
                bit = 2;
            }
            return money.setScale(bit, BigDecimal.ROUND_HALF_UP).doubleValue();
        }
        return 0.00D;
    }

    /**
     * 处理直接除报错
     *
     * @param divisor  除数
     * @param dividend 被除数
     * @return
     */
    public static BigDecimal divide(BigDecimal divisor, BigDecimal dividend) {
        if (dividend.compareTo(new BigDecimal(0)) == 0) {
            return new BigDecimal(0);
        }
        return divisor.divide(dividend, 5, RoundingMode.HALF_UP);
    }

    /**
     * 处理直接除报错
     *
     * @param divisor  除数
     * @param dividend 被除数
     * @return
     */
    public static Integer divideFloor(BigDecimal divisor, Integer dividend) {
        if (dividend == 0) {
            return 0;
        }
        return divisor.divide(new BigDecimal(dividend), 0, RoundingMode.FLOOR).setScale(0, BigDecimal.ROUND_HALF_DOWN).intValue();
    }

}
